package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Spider;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("SpiderController")
public class SpiderController extends CmiEmaController {

	@RequestMapping("/listSpiders")
	public ModelAndView listSpiders(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = spiderPaginationService.updatePagination(request, pagination);
		Set<Spider> spiders = spiderPaginationService.getPaginationSpiders(pagination);

		mav.addObject("spiders", spiders);
		mav.addObject("entityName", "spider");
		mav.addObject("pagination", pagination);
		mav.setViewName("spider/listSpiders");
		return mav;
	}

	public String listSpiders() {
		return "redirect:/listSpiders";
	}

	@RequestMapping("/viewSpider")
	public ModelAndView viewSpider(@RequestParam Integer spiderId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("spider", spiderDAO.findSpiderByPrimaryKey(spiderId));
		mav.setViewName("spider/viewSpider");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteSpiders")
	public ModelAndView confirmDeleteSpiders(
			@RequestParam(value = "deleteSpidersIds", required = true) Set<Integer> deleteSpidersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Spider> deleteSpiders = spiderDAO.findSpidersByIds(deleteSpidersIds);
		Set<Spider> referencedSpiders = spiderService.loadReferenceSpiders(deleteSpiders);
		boolean hasReferenced;
		if (referencedSpiders.size() == 0) {
			mav.addObject("deleteEntitys", deleteSpiders);
			hasReferenced = false;
		} else {
			deleteSpiders.removeAll(referencedSpiders);
			mav.addObject("referencedEntitys", referencedSpiders);
			mav.addObject("deleteEntitys", deleteSpiders);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Spider");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listSpiders");
		return mav;
	}

	@RequestMapping("/deleteSpider")
	public String deleteSpider(@RequestParam Integer spiderId) {
		Spider spider = spiderDAO.findSpiderByPrimaryKey(spiderId);
		spiderService.deleteSpider(spider);
		return "forward:/listSpiders";
	}

	@RequestMapping("/confirmDeleteSpider")
	public ModelAndView confirmDeleteSpider(@RequestParam Integer spiderId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("spider", spiderDAO.findSpiderByPrimaryKey(spiderId));

		mav.setViewName("spider/deleteSpider");

		return mav;
	}

	@RequestMapping("/deleteSpiders")
	public String deleteSpiders(
			@RequestParam(value = "deleteSpidersId", required = true) Set<Integer> deleteSpidersId) {
		spiderService.deleteSpiders(deleteSpidersId);
		return "forward:/listSpiders";
	}

	@RequestMapping("/editSpider")
	public ModelAndView editSpider(@RequestParam Integer spiderId) {

		ModelAndView mav = new ModelAndView();

		Spider spider = spiderDAO.findSpiderByPrimaryKey(spiderId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("spider", spider);

		// 把所有的引用spider对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(spiderService.loadSpiderObjectReferencedSet(spider));

		// 将编辑页面spider添加到mav中
		mav.setViewName("spider/editSpider");
		return mav;
	}

	@RequestMapping("/newSpider")
	public ModelAndView newSpider() {
		ModelAndView mav = new ModelAndView();
		Spider spider = new Spider();

		mav.addObject("spider", spider);

		// 把所有的引用spider对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(spiderService.loadSpiderObjectReferencedSet(spider));

		mav.addObject("newFlag", true);
		mav.setViewName("spider/editSpider");

		return mav;
	}

	@RequestMapping("/saveSpider")
	public ModelAndView saveSpider(@ModelAttribute Spider spider) {

		ModelAndView mav = new ModelAndView();

		Spider existingSpider = CmiHibernateProxyUtil
				.deproxy(spiderDAO.findSpiderByPhysicalPrimaryKey(spider.getName()));

		// 新建记录
		if (spider.getId() == null) {
			if (spider.equals(existingSpider)) {

				mav.addAllObjects(spiderService.loadSpiderClassReferencedSet(spider));
				// mav = editContinue(mav, spider);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("spider/editSpider");
			} else {
				spiderService.saveSpider(spider);
				mav.setViewName("forward:/listSpiders");
			}
		}
		// 编辑记录
		else {
			Spider originSpider = CmiHibernateProxyUtil.deproxy(spiderDAO.findSpiderByPrimaryKey(spider.getId()));
			if (!spider.equals(originSpider) && spider.equals(existingSpider)) {
				mav.addAllObjects(spiderService.loadSpiderClassReferencedSet(spider));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("spider/editSpider");
			} else {
				spiderService.saveSpider(spider);
				mav.setViewName("forward:/listSpiders");
			}
		}
		return mav;
	}

	@RequestMapping("/spiderController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
